<?php
/**
 * Created by PhpStorm.
 * User: Colin
 * Date: 2018/6/28
 * Time: 14:41
 */

namespace app\api\controller;
use think\Controller;
use think\Db;


class UserApps  extends Base
{
    public function __construct(){
        parent::__construct();
        $this->user=session('user');
    }
    //获取用户多个配置好模板
    public function getUserApps($name='',$pageNum=1,$pageSize=20)
    {
        $where=[];
        if($name!=''){
            $where['name']=['like','%'.$name.'%'];
        }
        $where['owner']=$this->user['id'];

        $data = Db::name('apps')->where($where)->field('id,owner,name,url,pic,des,date')->page($pageNum,$pageSize)->select();
        $count = Db::name('apps')->where($where)->count();

        return json(['code'=>200,'msg'=>'成功','data'=>$data,'count'=>$count,'success'=>true]);
    }
    //获取单个用户配置模板
    public function getUserApp($appid='')
    {
        if($appid==''){
            return result(500,'appid参数不能为空');
        }

        $where=[];
        $where['id']=$appid;
        $where['owner']=$this->user['id'];

        $data = Db::name('apps')->where($where)->find();

        return result(200,'成功',$data);
    }
    //删除用户模板
    public function delApp($id='')
    {
        if($id==''){
            return result(500,'id参数不能为空');
        }

        $where=[];
        $where['id']=$id;
        $where['owner']=$this->user['id'];

        $data = Db::name('apps')->where($where)->delete();

        return result(200,'删除成功');
    }

    //修改app文件
    public function saveData()
    {
        $params=input('post.');
        $shtml=trim($params['shtml']);
        if(empty($shtml)){
            return result(500,'shtml参数不能为空！');
        }


        $uid=$this->user['id'];

        $pathDir=ROOT_PATH.'public'.'/apps/'.$uid.'/'; //文件夹路径
        if(!is_dir($pathDir)){
            mkdir($pathDir,0777,true);
        }

//        $uid=$this->user['id'];

//        $pathDir=ROOT_PATH.'public'; //文件夹路径
//        mkdir($pathDir,0777,true);

//        //        $path=$pathDir.$appid.'.html';

////        $result = \app\library\File::saveFile($path,$shtml);

        $path=ROOT_PATH.'public'.'/apps/'.$uid.'/'.$params['id'].'/index.html';

        $result = \app\library\File::saveFile($path,$shtml);

        if(!$result){
            return result(500,'文件修改失败！');
        }

        $data=[
            'owner'=>$uid,
            'name'=>$params['name'],
            'url'=>'/apps/'.$uid.'/'.$params['id'].'.html',
            'pic'=>$params['pic'],
            'des'=>$params['des'],
            'date'=>date('Y-m-d H:i:s'),
            'data'=>$params['data'],
        ];

        Db::name('apps')->where(['id'=>$params['id']])->update($data);

        return result(200,'修改成功！');
    }

    //添加app
    public function addData()
    {
        $params=input('post.');
        //$shtml=trim($params['shtml']);
//        if(empty($shtml)){
//            return result(500,'shtml参数不能为空！');
//        }

        $uid=$this->user['id'];

        $pathDir=ROOT_PATH.'public'.'/apps/'.$uid.'/'; //文件夹路径
        if(!is_dir($pathDir)){
            mkdir($pathDir,0777,true);
        }

        $data=[
            'owner'=>$uid,
            'name'=>$params['name'],
            'url'=>$params['url'],
            'pic'=>$params['pic'],
            'des'=>$params['des'],
            'date'=>date('Y-m-d H:i:s'),
            'data'=>$params['data'],
        ];

        $appid = Db::name('apps')->insertGetId($data);

//        $path=$pathDir.$appid.'.html';

//        $result = \app\library\File::saveFile($path,$shtml);

//        if(!$result){
//            return result(500,'文件修改失败！');
//        }
        return result(200,'添加成功！',$appid);
//        return result(200,'添加成功！');
    }
}